#include "stdafx.h"
#include "BlendStateManager.h"
#include "GraphicsManager.h"

BlendStateManager::BlendStateManager(void)
{
	GraphicsManager* pGraphics = GraphicsManager::GetInstance();

	D3D11_BLEND_DESC blend_desc;
	ZeroMemory(&blend_desc, sizeof(D3D11_BLEND_DESC));

	for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
	{
		blend_desc.RenderTarget[i].BlendEnable = false;
		blend_desc.RenderTarget[i].SrcBlend = D3D11_BLEND_ONE;
		blend_desc.RenderTarget[i].DestBlend = D3D11_BLEND_ZERO;
		blend_desc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD;
		blend_desc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE;
		blend_desc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ZERO;
		blend_desc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD;
		blend_desc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	}

	blendStates.opaque = pGraphics->createBlendState(&blend_desc);

	ZeroMemory(&blend_desc, sizeof(D3D11_BLEND_DESC));

	for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
	{
		blend_desc.RenderTarget[i].BlendEnable = true;
		blend_desc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_ALPHA;
		blend_desc.RenderTarget[i].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
		blend_desc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD;

		blend_desc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_ONE;
		blend_desc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_ONE;
		blend_desc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD;
		blend_desc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	}

	blendStates.alphaBlend = pGraphics->createBlendState(&blend_desc);

	ZeroMemory(&blend_desc, sizeof(BlendState));

	for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
	{
		blend_desc.RenderTarget[i].BlendEnable = true;
		blend_desc.RenderTarget[i].SrcBlend = D3D11_BLEND_SRC_COLOR;
		blend_desc.RenderTarget[i].DestBlend = D3D11_BLEND_DEST_COLOR;
		blend_desc.RenderTarget[i].BlendOp = D3D11_BLEND_OP_ADD;

		blend_desc.RenderTarget[i].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
		blend_desc.RenderTarget[i].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
		blend_desc.RenderTarget[i].BlendOpAlpha = D3D11_BLEND_OP_ADD;
		blend_desc.RenderTarget[i].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
	}

	blendStates.additive = pGraphics->createBlendState(&blend_desc);
}

BlendStateManager::~BlendStateManager(void)
{
	blendStates.alphaBlend->Release();
	blendStates.opaque->Release();
}

void BlendStateManager::SetBlendState(BlendState* blendState)
{
	GraphicsManager::GetInstance()->setBlendState(blendState);
}

BlendState* BlendStateManager::GetBlendState(void) const
{
	BlendState* blendState;
	float blendFactor[4];
	GraphicsManager::GetInstance()->getDeviceContext()->OMGetBlendState(&blendState, blendFactor, 0);

	return blendState;
}